﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PRODUCT.Product.database;

namespace ERP_RORZE_PRODUCT.Product.Machine
{
    public partial class frm_MachineEdit : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler Reload;
        ClsMachineDAO clsmachineDAO = new ClsMachineDAO();
        MachineTable b = new MachineTable();
        private LogUtils log;
        DataTable dt;

        public string ID;

        public frm_MachineEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtMachineID.Text == "")
            {
                MessageBox.Show("Chưa nhập MachineID");
            }
            else if (txtMakerMachineNo.Text == "")
            {
                MessageBox.Show("Chưa nhập MakerMachine");
            }
            else
            {
                try
                {
                    b.AccName = txtAccName.Text;
                    b.Charge = decimal.Parse(txtCharge.Text);
                    b.DensanCode = txtDensanCode.Text;
                    b.DensanName = txtDensanName.Text;
                    b.Dmachine = txtDmachine.Text;
                    b.Kakokikai = checkkakokikai.Checked;
                    b.MachineGR = txtMachineGR.Text;
                    b.MachineID = txtMachineID.Text;
                    b.MachineName = txtMachineName.Text;
                    b.Maker = txtMaker.Text;
                    b.MakerMachineNo = txtMakerMachineNo.Text;
                    b.MultiPalet = checkMultiPalet.Checked;
                    b.Ratio = float.Parse(txtRatio.Text);
                    clsmachineDAO.UpdateMachine(b);

                    //write Log
                    log.Table = LogConstant.Table.Product.MachineList;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "MachineID: " + b.MachineID;
                    log.WriteLog();

                    MessageBox.Show("Đã cập nhật thành công");
                    Reload(sender, e);
                }
                catch
                {
                    MessageBox.Show("Charge và Ratio phải là kiểu số.");
                }
            }
        }

        private void frm_MachineEdit_Load(object sender, EventArgs e)
        {
            b.MachineID = ID;
            dt = clsmachineDAO.GetMachineByID(b);
            foreach(DataRow r in dt.Rows)
            {
                txtAccName.Text = r["AccName"].ToString();
                if(r["Charge"].ToString() == "")
                {
                    txtCharge.Text = "0";
                }
                else
                {
                txtCharge.Text = r["Charge"].ToString();
                }
                txtDensanCode.Text = r["DensanCode"].ToString();
                txtDensanName.Text = r["DensanName"].ToString();
                txtDmachine.Text = r["Dmachine"].ToString();
                txtMachineGR.Text = r["MachineGR"].ToString();
                txtMachineID.Text = r["MachineID"].ToString();
                txtMachineName.Text = r["MachineName"].ToString();
                txtMaker.Text = r["Maker"].ToString();
                txtMakerMachineNo.Text = r["MakerMachineNo"].ToString();
                if (r["Ratio"].ToString() == "")
                {
                    txtRatio.Text = "0";
                }
                else
                {
                txtRatio.Text = r["Ratio"].ToString();
                }
                checkkakokikai.Checked = bool.Parse(r["kakokikai"].ToString());
                checkMultiPalet.Checked = bool.Parse(r["MultiPalet"].ToString());
            }
        }
    }
}